home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / source / snip9503 / bastrngs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-14  |  3.5 KB  |  175 lines

  1. /*
  2. **  BASIC-like string operations
  3. **
  4. **  public domain by Bob Stout
  5. */
  6.  
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <stdlib.h>
  10. #include <stdarg.h>
  11. #include <limits.h>
  12. #include <assert.h>
  13.  
  14. #define NUL '\0'
  15.  
  16. static int stralloc_ptr;
  17. static char *strings[8];
  18. static int str_tag[8];
  19.  
  20. /*
  21. **  stralloc() is the key function in this package, maintaining a pool of
  22. **  reusable strings.
  23. */
  24.  
  25. char *stralloc(size_t length)
  26. {
  27.       register int i;
  28.  
  29.       if (UINT_MAX == length)       /* Assume size_t == unsigned int    */
  30.             return NULL;
  31.  
  32.       i = stralloc_ptr++;
  33.       ++length;                     /* Allow for terminating NUL        */
  34.  
  35.       if ((!strings[i]) || (length > strlen(strings[i])))
  36.       {
  37.             strings[i] = (char *)realloc(strings[i], length);
  38.             assert(NULL != strings[i]);
  39.             str_tag[i] = -1;
  40.       }
  41.       else  str_tag[i] = 0;
  42.       stralloc_ptr &= 7;
  43.       return (strings[i]);
  44.       /* Maintains 8 strings in a circular buffer */
  45. }
  46.  
  47. /*
  48. **  free the string pool.
  49. */
  50.  
  51. void str_free(char *string)
  52. {
  53.       register int i;
  54.  
  55.       for (i = 0; i < 8; ++i)
  56.       {
  57.             if (strings[i] == string)
  58.             {
  59.                   if (str_tag[i])
  60.                         free(strings[i]);
  61.                   return;
  62.             }
  63.       }
  64. }
  65.  
  66. /*
  67. **  return the leftmost N characters from a string, equivalent to LEFT$
  68. */
  69.  
  70. char *left(char *string, size_t N)
  71. {
  72.       char *buf;
  73.       size_t strlength = strlen(string);
  74.  
  75.       if (N > strlength)
  76.             N = strlength;
  77.       buf = stralloc(N);
  78.       memcpy(buf, string, N);
  79.       buf[N] = NUL;
  80.       return buf;
  81. }
  82.  
  83. /*
  84. **  return the rightmost N characters from a string, equivalent to RIGHT$
  85. */
  86.  
  87. char *right(char *string, size_t N)
  88. {
  89.       char *buf;
  90.       size_t strlength = strlen(string);
  91.  
  92.       if (N > strlength)
  93.             N = strlength;
  94.       buf = stralloc(N);
  95.       strcpy(buf, &string[strlength-N]);
  96.       return buf;
  97. }
  98.  
  99. /*
  100. **  return a substring, N characters long beginning at position M,
  101. **  equivalent to MID$
  102. */
  103.  
  104. char *mid(char *string, size_t M, size_t N)
  105. {
  106.       char *buf;
  107.       size_t strlength = strlen(string);
  108.  
  109.       if (M > strlength)
  110.             return NULL;
  111.       if (N > (strlength - M))
  112.             N = strlength - M;
  113.       buf = stralloc(N);
  114.       memcpy(buf, &string[M-1], N);
  115.       buf[N] = NUL;
  116.       return buf;
  117. }
  118.  
  119. /*
  120. **  string concatenation function, equivalent to A$=B$+C$+...
  121. */
  122.  
  123. char *string_add(char *string, ...)
  124. {
  125.       va_list arg_ptr;
  126.       char *temp1, *temp2, *buf;
  127.  
  128.       va_start(arg_ptr, string);
  129.       temp1 = string;
  130.       do
  131.       {
  132.             if(NULL == (temp2 = va_arg(arg_ptr, char *)))
  133.                   break;
  134.             buf = stralloc(strlen(temp1) + strlen(temp2));
  135.             temp1 = strcat(strcpy(buf, temp1), temp2);
  136.       } while (NULL != temp2);
  137.       return temp1;
  138. }
  139.  
  140. /*
  141. **  create a string of repeated characters, equivalent to STRING$()
  142. */
  143.  
  144. char *string(int ch, size_t N)
  145. {
  146.       char *buf;
  147.  
  148.       if (N)
  149.       {
  150.             buf = stralloc(N);
  151.             memset(buf, ch, N);
  152.       }
  153.       buf[N] = NUL;
  154.       return buf;
  155.                   
  156. }
  157.  
  158. #ifdef TEST
  159.  
  160. /*
  161. **  Demo main()
  162. */
  163.  
  164. main()
  165. {
  166.       char *x = "European", *y = "Hardware", *z = "Skaters", *q;
  167.  
  168.       z = string_add(left(x, 2), right(y, 2), mid(z, 2, 2), "!", NULL);
  169.       q = string('!', 4);
  170.       printf("%s%s\n", z, q);
  171.       return 0;
  172. }
  173.  
  174. #endif /* TEST */
  175.